/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AssignerComputerRoom.java
 *
 * Created on 20/06/2011, 07:39:19 PM
 */

package GUI.ATTENDANT;

import GUI.Customer.Server;
import Procedures.DBConection;
import Procedures.LoanTable;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author SIX-14
 */
public class AssignerComputerRoom extends javax.swing.JPanel {

    /** Creates new form AssignerComputerRoom */
    public AssignerComputerRoom() {
        initComponents();
    }

    public AssignerComputerRoom(DBConection myConection, JFrame paternFrame, LoanTable loantable, Server server) {
        initComponents();
        this.myConection=myConection;
        this.paternFrame=paternFrame;
        this.loanTable=loantable;
        this.server=server;
        fillComboBox();
    }


    private DBConection myConection;
    private JFrame paternFrame;
    private LoanTable loanTable;
    private Server server;

    public Server getServer() {
        return server;
    }

    public void setServer(Server server) {
        this.server = server;
    }
    
    public LoanTable getLoanTable() {
        return loanTable;
    }

    public void setLoanTable(LoanTable loanTable) {
        this.loanTable = loanTable;
    }

    public DBConection getMyConection() {
        return myConection;
    }

    public void setMyConection(DBConection myConection) {
        this.myConection = myConection;
    }

    public JFrame getPaternFrame() {
        return paternFrame;
    }

    public void setPaternFrame(JFrame paternFrame) {
        this.paternFrame = paternFrame;
    }

    public void fillComboBox(){
        getMyConection().sendSQLQuery("SELECT LETTER FROM COMPUTER_ROOM where active=1;");
        String[] elements = getMyConection().getLastResultSetColumnStrings(1);
        if (elements != null) {
            jComboBox2.removeAllItems();
            jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(elements));
        }
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();

        jLabel1.setText("Identificador");

        jLabel3.setText("Letra de sala");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jButton1.setText("Prestar");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addContainerGap(328, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(334, Short.MAX_VALUE))))
            .addGroup(layout.createSequentialGroup()
                .addGap(76, 76, 76)
                .addComponent(jButton1)
                .addContainerGap(257, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addComponent(jButton1)
                .addContainerGap(132, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked

                if(!getLoanTable().find_user(Integer.parseInt(jTextField1.getText()), "Sala")){
            getMyConection().sendSQLQuery("SELECT ACTIVE FROM TEACHER WHERE ID="+jTextField1.getText()+";");
            String[] valor=getMyConection().getLastResultSetColumnStrings(1);
            if(valor!=null){
                if(Boolean.parseBoolean(valor[0])){
                    getMyConection().sendSQLQuery("SELECT NUMBER, IP FROM COMPUTER WHERE LETTERCOMPUTERROOM='"+jComboBox2.getSelectedItem().toString()+"' AND STATE=1;");
                    String[] number=getMyConection().getLastResultSetColumnStrings(1);
                    if(number!=null){
                        String[] ip=getMyConection().getLastResultSetColumnStrings(2);
                        getMyConection().sendSQLQuery("SELECT ID FROM APLICATION;");
                        String[] process=getMyConection().getLastResultSetColumnStrings(1);

                        for(int i=0; i<number.length;i++){
                            getLoanTable().addLoan(Integer.parseInt(jTextField1.getText()), "Sala", jComboBox2.getSelectedItem().toString(), ip[i], Integer.parseInt(number[i]));
                            getServer().getComstumer().beginMonitorCostumer("/"+ip[i], process);
                        }
                        JOptionPane.showMessageDialog(null, "Prestamo realisado Exitosamente", " Prestamo Exitosoo ", 1);
                        getMyConection().sendSQL("UPDATE COMPUTER SET STATE=5 WHERE LETTERCOMPUTERROOM='"+jComboBox2.getSelectedItem().toString()+"';");
                        this.setVisible(false);
                        getPaternFrame().getContentPane().remove(this);
                    }

                }else{
                    JOptionPane.showMessageDialog(null, "Usuario no esta abilitado ", " Usuario inabilitado ", 1);
                }
            }else{
                JOptionPane.showMessageDialog(null, "Usuario no esta regsitrado ", " Usuario no registrado ", 1);
            }
        }else{
            JOptionPane.showMessageDialog(null, "Usuario quiere duplicar prestamo ", " Duplicado de prestamo ", 1);
        }
    }//GEN-LAST:event_jButton1MouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

}
